home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / cagd_lib / cagd_wrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  6.7 KB  |  230 lines

  1. /******************************************************************************
  2. * Cagd_Wrt.c - Generic Curve/Surface writing to files.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, July. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef USE_VARARGS
  8. #    include <varargs.h>
  9. #else
  10. #    include <stdarg.h>
  11. #endif /* USE_VARARGS */
  12.  
  13. #include <string.h>
  14. #include "cagd_loc.h"
  15. #include "irit_soc.h"
  16.  
  17. static int
  18.     GlblWriteSocket = FALSE;               /* If TRUE - write to socket. */
  19. static CagdPrintfFuncType
  20.     RedirCagdPrintfFunc = NULL;
  21.  
  22. /*****************************************************************************
  23. * Same as fprintf but with indentation.                         *
  24. *****************************************************************************/
  25. void CagdSetCagdFprintf(CagdPrintfFuncType Func)
  26. {
  27.     RedirCagdPrintfFunc = Func;
  28. }
  29.  
  30. /*****************************************************************************
  31. * Same as fprintf but with indentation.                         *
  32. *****************************************************************************/
  33. #ifdef USE_VARARGS
  34. void _CagdFprintf(FILE *f, int Indent, char *va_alist, ...)
  35. {
  36.     static char AccumLine[LINE_LEN_LONG];
  37.     char *p, *Format, Line[LINE_LEN_LONG];
  38.     int i;
  39.     va_list ArgPtr;
  40.  
  41.     va_start(ArgPtr);
  42.     Format = va_arg(ArgPtr, char *);
  43. #else
  44. void _CagdFprintf(FILE *f, int Indent, char *Format, ...)
  45. {
  46.     static char AccumLine[LINE_LEN_LONG];
  47.     char *p, Line[LINE_LEN_LONG];
  48.     int i;
  49.     va_list ArgPtr;
  50.  
  51.     va_start(ArgPtr, Format);
  52. #endif /* USE_VARARGS */
  53.  
  54.     vsprintf(Line, Format, ArgPtr);
  55.     va_end(ArgPtr);
  56.  
  57.     if (GlblWriteSocket) {
  58.     /* No need for intentation if writing to a socket. */
  59.     SocServerWriteLine(Line, strlen(Line));
  60.     }
  61.     else if (RedirCagdPrintfFunc && f == NULL) {
  62.     for (i = 0, p = &AccumLine[strlen(AccumLine)]; i < Indent; i++)
  63.         *p++ = ' ';
  64.     *p = 0;
  65.  
  66.     if ((p = strchr(Line, '\n')) != NULL ||
  67.         (p = strchr(Line, '\r')) != NULL)
  68.         *p = 0;
  69.     strcat(AccumLine, Line);
  70.     if (p != NULL) {
  71.         RedirCagdPrintfFunc(AccumLine);
  72.         AccumLine[0] = 0;
  73.     }
  74.     }
  75.     else {
  76.     for ( ; Indent >= 8; Indent -= 8)
  77.         fputc('\t', f);
  78.     while (Indent--)
  79.         fputc(' ', f);
  80.     fputs(Line, f);
  81.     }
  82. }
  83.  
  84. /*****************************************************************************
  85. * Generic routine to write curve(s) to the given file.                 *
  86. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  87. *****************************************************************************/
  88. int CagdCrvWriteToFile2(CagdCrvStruct *Crvs, FILE *f, int Indent, char *Comment,
  89.                                 char **ErrStr)
  90. {
  91.     int RetVal = TRUE;
  92.     CagdCrvStruct *NextCrv;
  93.  
  94.     for (; Crvs != NULL && RetVal; Crvs = Crvs -> Pnext) {
  95.     NextCrv = Crvs -> Pnext;      /* To make sure we dump one at a time. */
  96.     Crvs -> Pnext = NULL;
  97.  
  98.     switch(Crvs -> GType) {
  99.         case CAGD_CBEZIER_TYPE:
  100.         RetVal = BzrCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  101.         break;
  102.         case CAGD_CBSPLINE_TYPE:
  103.         RetVal = BspCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  104.         break;
  105.         case CAGD_CPOWER_TYPE:
  106.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  107.         return FALSE;
  108.         default:
  109.         FATAL_ERROR(CAGD_ERR_UNDEF_CRV);
  110.         return FALSE;
  111.     }
  112.  
  113.     Crvs -> Pnext = NextCrv;
  114.     }
  115.  
  116.     return RetVal;
  117. }
  118.  
  119. /*****************************************************************************
  120. * Generic routine to write surface(s) to the given file.             *
  121. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  122. *****************************************************************************/
  123. int CagdSrfWriteToFile2(CagdSrfStruct *Srfs, FILE *f, int Indent, char *Comment,
  124.                                 char **ErrStr)
  125. {
  126.     int RetVal = TRUE;
  127.     CagdSrfStruct *NextSrf;
  128.  
  129.     for (; Srfs != NULL && RetVal; Srfs = Srfs -> Pnext) {
  130.     NextSrf = Srfs -> Pnext;      /* To make sure we dump one at a time. */
  131.     Srfs -> Pnext = NULL;
  132.  
  133.     switch (Srfs -> GType) {
  134.         case CAGD_SBEZIER_TYPE:
  135.         RetVal = BzrSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  136.         break;
  137.         case CAGD_SBSPLINE_TYPE:
  138.         RetVal = BspSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  139.         break;
  140.         case CAGD_SPOWER_TYPE:
  141.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  142.         return FALSE;
  143.         default:
  144.         FATAL_ERROR(CAGD_ERR_UNDEF_SRF);
  145.         return FALSE;
  146.     }
  147.  
  148.     Srfs -> Pnext = NextSrf;
  149.     }
  150.  
  151.     return RetVal;
  152. }
  153.  
  154. /*****************************************************************************
  155. * Generic routine to write curve(s) to the given file.                 *
  156. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  157. *****************************************************************************/
  158. int CagdCrvWriteToFile(CagdCrvStruct *Crvs, char *FileName, int Indent,
  159.                          char *Comment, char **ErrStr)
  160. {
  161.     int RetVal = TRUE;
  162.     CagdCrvStruct *NextCrv;
  163.  
  164.     for (; Crvs != NULL && RetVal; Crvs = Crvs -> Pnext) {
  165.     NextCrv = Crvs -> Pnext;      /* To make sure we dump one at a time. */
  166.     Crvs -> Pnext = NULL;
  167.  
  168.     switch(Crvs -> GType) {
  169.         case CAGD_CBEZIER_TYPE:
  170.         RetVal = BzrCrvWriteToFile(Crvs, FileName, Indent, Comment, ErrStr);
  171.         break;
  172.         case CAGD_CBSPLINE_TYPE:
  173.         RetVal = BspCrvWriteToFile(Crvs, FileName, Indent, Comment, ErrStr);
  174.         break;
  175.         case CAGD_CPOWER_TYPE:
  176.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  177.         default:
  178.         FATAL_ERROR(CAGD_ERR_UNDEF_CRV);
  179.         return FALSE;
  180.     }
  181.  
  182.     Crvs -> Pnext = NextCrv;
  183.     }
  184.  
  185.     return RetVal;
  186. }
  187.  
  188. /*****************************************************************************
  189. * Generic routine to write surface(s) to the given file.             *
  190. ( If Comment is NULL, no comment is printed, if "" only internal coment.     *
  191. *****************************************************************************/
  192. int CagdSrfWriteToFile(CagdSrfStruct *Srfs, char *FileName, int Indent,
  193.                          char *Comment, char **ErrStr)
  194. {
  195.     int RetVal = TRUE;
  196.     CagdSrfStruct *NextSrf;
  197.  
  198.     for (; Srfs != NULL && RetVal; Srfs = Srfs -> Pnext) {
  199.     NextSrf = Srfs -> Pnext;      /* To make sure we dump one at a time. */
  200.     Srfs -> Pnext = NULL;
  201.  
  202.     switch (Srfs -> GType) {
  203.         case CAGD_SBEZIER_TYPE:
  204.         RetVal = BzrSrfWriteToFile(Srfs, FileName, Indent, Comment, ErrStr);
  205.         break;
  206.         case CAGD_SBSPLINE_TYPE:
  207.         RetVal = BspSrfWriteToFile(Srfs, FileName, Indent, Comment, ErrStr);
  208.         break;
  209.         case CAGD_SPOWER_TYPE:
  210.         FATAL_ERROR(CAGD_ERR_POWER_NO_SUPPORT);
  211.         return FALSE;
  212.         default:
  213.         FATAL_ERROR(CAGD_ERR_UNDEF_SRF);
  214.         return FALSE;
  215.     }
  216.  
  217.     Srfs -> Pnext = NextSrf;
  218.     }
  219.  
  220.     return RetVal;
  221. }
  222.  
  223. /*****************************************************************************
  224. *   Routine to request writing to a socket (if non zero) instead of a file.  *
  225. *****************************************************************************/
  226. void CagdWriteSocket(int WriteSocket)
  227. {
  228.     GlblWriteSocket = WriteSocket;
  229. }
  230.